package com.bai.Service.Impl;

import com.bai.Constant.ResultConst;
import com.bai.Mapper.AdminMapper;
import com.bai.Pojo.Admin;
import com.bai.Service.AdminService;
import com.bai.Utils.DtoResultUtil;
import com.bai.dto.PageLimitDto;
import com.bai.dto.PublicResponseBody;
import org.springframework.transaction.annotation.Transactional;

/**
 * @title: adminServiceImpl
 * @Author BaiSc
 * @Date: 2021/6/5 16:12
 * @Version 1.0
 */
@Transactional
public class AdminServiceImpl implements AdminService {

    private AdminMapper adminMapper;

    public void setAdminMapper(AdminMapper adminMapper) {
        this.adminMapper = adminMapper;
    }


  /*  public PublicResponseBody LoginByUser(String username, String password) {
        Admin loginByUser = adminMapper.LoginByUser(username, password);
        if (loginByUser!=null){
            return DtoResultUtil.resultSuccess();
        }else {
            return DtoResultUtil.result(-1,ResultConst.add_error);
        }
    }*/

    public PageLimitDto queryAllAdmin(Integer offset, Integer limit) {
        PageLimitDto pageLimitDto=new PageLimitDto();
        pageLimitDto.setTotal(adminMapper.queryAllAdminCount());
        pageLimitDto.setRows(adminMapper.queryAllAdmin(offset,limit));
        return pageLimitDto;
    }

    @Override
    public PublicResponseBody deleteAdmin(Integer id) {
        int i = adminMapper.deleteAdmin(id);
        if (i>0){
            return DtoResultUtil.resultSuccess();
        }else {
            return DtoResultUtil.result(-1,ResultConst.delete_error);
        }
    }
    //添加
    @Override
    public PublicResponseBody addAdmin(Admin admin) {
        int addAdmin = adminMapper.addAdmin(admin);
        if (addAdmin>0){
            return DtoResultUtil.resultSuccess();
        }else {
            return DtoResultUtil.result(-1,ResultConst.add_error);
        }
    }

    @Override
    public PublicResponseBody editAdmin(Admin admin) {
        int addAdmin = adminMapper.editAdmin(admin);
        if (addAdmin>0){
            return DtoResultUtil.resultSuccess();
        }else {
            return DtoResultUtil.result(-1,ResultConst.edit_error);
        }
    }

    @Override
    public PublicResponseBody getAdmin(int id) {
        Admin admin = adminMapper.getAdmin(id);
        if (admin!=null){
            return DtoResultUtil.resultSuccess(admin);
        }else {
            return DtoResultUtil.result(-1,ResultConst.query_error);
        }
    }
}
